home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / grabtext.zip / PTXMAKE.C < prev    next >
Text File  |  1990-08-20  |  8KB  |  288 lines

  1. /*
  2.    PTXMAKE.c by Bill Buckels 1990
  3.    written in AZTEC C small model
  4.  
  5.    Supported Screen Mode is CGA COLOR TEXT Mode 4.
  6.  
  7.   This is a companion utility to be used with BSAVED text images
  8.   and with my .PTX fileviewer PTXVU.EXE which was also written for fun.
  9.   I hope this code proves informative to those of you interested in
  10.   computer graphics.
  11.  
  12.   This is also a limited example of applied "Run Length Encoding."
  13.  
  14.   The IBM-PC 80- column text screen buffer-attribute pair arrangement
  15.   is suited to a 2-pass crunching algorithm.
  16.  
  17.   In purely poetic terms....
  18.  
  19.   Blocks of repeats occur on increments of every second byte so
  20.   I leapfrog through the file using the .PCX encoding technique
  21.   but I skip every second byte, and encode the ascuii value of
  22.   the text character only, then when I am done I make a second
  23.   pass to encode the bytes that have been skipped on first pass,
  24.   encoding the screen attributes.
  25.  
  26.   Function:
  27.  
  28.   converts a BASIC BSAVED .BSV IMAGE FILE (full screen CGA text)
  29.   to a .PTX ( similar to the ZSOFT .PCX format image file per the
  30.   VERSION 2.8 STANDARD without Color Map.)
  31.  
  32.  
  33. */
  34.  
  35. #include <stdio.h>
  36. #include <fcntl.h>
  37. #include <stdlib.h>
  38. #include <io.h>
  39.  
  40. #define SCREENSIZE 4000
  41.  
  42. FILE *ptxfile;
  43.  
  44. /* type conversion functions */
  45. unsigned char lsb(unsigned int word){ return word &0xff;}
  46. unsigned char msb(unsigned int word){ return word >>8;}
  47.  
  48. unsigned char ptxheader[128];
  49.  
  50. /* a microsoft compatible bsaved memory image format descriptor */
  51. unsigned char BSV_header[7]={
  52.  
  53.     '\xfd',          /* ID Flag = file descriptor identifier bsaved file */
  54.  
  55.    /* afew words about what BASIC does with this header */
  56.    /* and afew words about extending BASIC's ability to */
  57.    /* read and use BINARY files created in this manner. */
  58.  
  59.     /* BASIC will use original segment and offset information  */
  60.     /* to reload a memory image unless "DEF SEG" has been used */
  61.     /* and then an explicit offset is used as the second arg   */
  62.     /* of the bload command. */
  63.  
  64.     /* subsequently, once DEF SEG is invoked  */
  65.     /* If an offset is specified without then */
  66.     /* first calling DEF SEG, The image will then be loaded to */
  67.     /* the segment specified in the last segment pointed to    */
  68.     /* by the last call to DEF SEG. DEF SEG without args returns */
  69.     /* to DGROUP (the default data segment) and resets the thing.*/
  70.  
  71.     /* we can also implement and store an array in memory by use of */
  72.     /* VARSEG and VARPTR to obtain the window for the array's       */
  73.     /* memory location, i.e. override the defaults by windowing     */
  74.     /* to the array base using DEF SEG = VARSEG(arrayname) then to     */
  75.     /* fill the array we would use BLOAD bsaved.bsv, VARPTR(arrayname) */
  76.     /* using VARPTR to point to the offset from the memory base segment*/
  77.  
  78.     '\x00', '\xb8',  /* base address     = MSB | LSB original segment    */
  79.     '\x00', '\x00',  /* offset from base = MSB | LSB original offset     */
  80.  
  81.     '\xA0', '\x0F'   /* file size = MSB | LSB of bytes to be loaded +    */
  82.                      /* size of descriptors in bytes (8)                 */
  83.     };
  84.  
  85. unsigned char BSAVED_tailer[1]={   /* for reference only */
  86.     '\x1A'
  87.     };
  88.  
  89.  
  90. int encline(inbuff,inlen)/* encodes a raw line and writes it out */
  91. unsigned char *inbuff;
  92. int inlen;
  93. {
  94.     unsigned char this,last;
  95.     int srcindex,i;
  96.     register int total;
  97.     register unsigned char runcount;
  98.     total=0;
  99.     last = *(inbuff); runcount=1;
  100.  
  101. for(srcindex=1;srcindex!=inlen;srcindex++){
  102.     this= *(++inbuff);
  103.     if(this==last){
  104.         runcount++;
  105.         if(runcount==63){
  106.             if(!(i=encput(last,runcount)))
  107.             return(0);
  108.             total+=i;
  109.             runcount=0;
  110.         }
  111.     }
  112.     else{
  113.         if(runcount){
  114.             if(!(i=encput(last,runcount)))
  115.             return(0);
  116.             total+=i;
  117.         }
  118.         last=this;
  119.         runcount=1;
  120.       }
  121.    }
  122.  
  123. if(runcount){
  124.     if(!(i=encput(last,runcount)))
  125.     return(0);
  126.     return(total+i);
  127.     }
  128.     return (total);
  129.  
  130. }
  131.  
  132. int encput(byt,cnt)       /* the writer for the encline function */
  133. unsigned char byt,cnt;
  134. {
  135.           if(cnt){
  136.             if((cnt==1)&& (0xc0 != (0xc0 &byt))){
  137.                 if(EOF == fputc((int)byt,ptxfile))
  138.                 return(0);
  139.                 return(1);
  140.             }
  141.             else{
  142.                 if(EOF ==fputc((int)0xc0|cnt,ptxfile))
  143.                 return(0);
  144.                 if(EOF ==fputc((int)byt,ptxfile))
  145.                 return(0);
  146.                 return(2);
  147.             }
  148.         }
  149.         return(0);
  150.  
  151. }
  152.  
  153.  
  154. /* the zsoft technical reference manual details the */
  155. /* structure of the following header */
  156. int makeheader()
  157. {
  158.     /* makes a standard header for a .PTX CGA FULL-SCREEN TEXT FILE    */
  159.     /* since this info is not really intended for text mode, we fudge  */
  160.     /* some portions of the info, but we try to be faithful as possible*/
  161.  
  162.     int i;
  163.     unsigned char notzsoft=0,version=3,codetype=1,pixbits=16;
  164.     unsigned int  xmin=0, ymin=0, xmax=79, ymax=24;
  165.     unsigned int  hres=80, vres=25;
  166.     unsigned char no_planes=1;
  167.     unsigned int  bytesperline=160;
  168.  
  169.     for(i=0;i!=128;i++)ptxheader[i]=0;/* pad the header with nulls */
  170.  
  171.     ptxheader[0]=notzsoft;
  172.     ptxheader[1]=version;
  173.     ptxheader[2]=codetype;
  174.     ptxheader[3]=pixbits;
  175.     ptxheader[4] =lsb(xmin);
  176.     ptxheader[5] =msb(xmin);
  177.     ptxheader[6] =lsb(ymin);
  178.     ptxheader[7] =msb(ymin);
  179.     ptxheader[8] =lsb(xmax);
  180.     ptxheader[9] =msb(xmax);
  181.     ptxheader[10]=lsb(ymax);
  182.     ptxheader[11]=msb(ymax);
  183.     ptxheader[12]=lsb(hres);
  184.     ptxheader[13]=msb(hres);
  185.     ptxheader[14]=lsb(vres);
  186.     ptxheader[15]=msb(vres);
  187.     ptxheader[65]=no_planes;
  188.     ptxheader[66]=lsb(bytesperline);
  189.     ptxheader[67]=msb(bytesperline);
  190.     return 0;
  191.  
  192. }
  193.  
  194. int BSV2PTX(char *name1, char *name2)
  195. {
  196.     unsigned char *scratchbuffer;
  197.     unsigned char *buf;
  198.  
  199.     unsigned char header[7];
  200.     int i,j,k;
  201.  
  202.     int fh;
  203.  
  204.     if((fh = open(name1,O_RDONLY)) == -1)return -1;
  205.     read(fh,header,7);
  206.  
  207.     /* make sure that we have a valid BSAVED file format */
  208.     /* we can only check the first 2-bytes since it may  */
  209.     /* have been made on the HERCULES */
  210.  
  211.     if(header[0]!=BSV_header[0] || header[1]!=BSV_header[1])
  212.         {
  213.          close(fh);
  214.          return -2;
  215.          }
  216.  
  217.     printf("BSV2PTX(C) Copyright by Bill Buckels\n\n");
  218.     printf("Input  File: %s\n",name1);
  219.     printf("Output File: %s\n",name2);
  220.  
  221.     scratchbuffer= malloc(SCREENSIZE);
  222.     buf=malloc(SCREENSIZE/2);
  223.  
  224.     read(fh,scratchbuffer,SCREENSIZE);
  225.     close(fh);
  226.  
  227.     /* seperate the attribute bytes */
  228.     /* use the two-tank system.     */
  229.  
  230.     makeheader();
  231.     ptxfile=fopen(name2,"w");
  232.     for(i=0;i!=128;i++)fputc(ptxheader[i],ptxfile);/* write the header */
  233.  
  234.                 for(k=0;k!=2;k++)
  235.                    {
  236.                     j=0;
  237.                     for(i=k;i<SCREENSIZE;i+=2)
  238.                     {
  239.                      buf[j]=scratchbuffer[i];
  240.                      j++;
  241.                      }
  242.                      encline(buf,SCREENSIZE/2);
  243.                      }
  244.  
  245.             fclose(ptxfile);
  246.             free(scratchbuffer);
  247.             free(buf);
  248.             printf("Done!\n");
  249.             return 0;
  250. }
  251.  
  252. main(int argc,char *argv[])
  253. {
  254.    char buffer[128];
  255.    char  name1[128];
  256.    char  name2[128];
  257.    char *wordptr;
  258.    int status=0;
  259.  
  260.           switch(argc)
  261.           {
  262.             case 2: strcpy(buffer,argv[1]);
  263.  
  264.                     while(buffer[status]!=0)
  265.                           {
  266.                             if(buffer[status]=='.')buffer[status]=0;
  267.                             else status++;
  268.                             }
  269.  
  270.                     strcpy(name1,buffer);
  271.                     strcat(name1,".BSV");
  272.                     strcpy(name2,buffer);
  273.                     strcat(name2,".PTX");
  274.  
  275.                         status=BSV2PTX(name1,name2);
  276.                         if(status==0)exit(0);
  277.  
  278.  default: printf("PTXMAKE(C) Copyright by Bill Buckels 1990\n");
  279.           printf(
  280.  "Usage : \"PTXMAKE [filename]\"\n");
  281.           printf(
  282.           "Converts .BSV BSAVED IMAGES to .PTX PACKED TEXT IMAGES.\n");
  283.           printf("CGA COLOR TEXT mode, screen dumps only.\n");
  284.           }
  285.           exit(0);
  286. }
  287.  
  288.